﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using HtmlAgilityPack;
using System.Collections.Generic;
using System.Collections;
using System.Threading;
using YinYueTai.Model;
using System.Windows.Media.Imaging;
using HaoLab.WindowsPhone;

namespace YinYueTai.HtmlParser
{
    public class Focus
    {
        private Action<List<MV>> act;

        private string page;

        public Focus(string page, Action<List<MV>> act)
        {
            this.act = act;
            this.page = page ?? "";
        }

        public void GetInfo()
        {
            GlobalIndicator.Instance.IsBusy = true;
            GlobalIndicator.Instance.Text = "正在加载...";

          
            WebClient wc = new WebClient();
            wc.Headers["Accept"] = "text/html, application/xhtml+xml, */*";
            //wc.Headers["Accept-Encoding"] = "gzip, deflate";
            wc.Headers["User-Agent"] = "Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; Microsoft; XDeviceEmulator)";
            wc.Headers["Accept-Language"] = "zh-cn,en-us;q=0.5";
            wc.Headers["UA-CPU"] = "x86";
            wc.Headers["Referer"] = "http://www.yinyuetai.com/";

            wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
            wc.DownloadStringAsync(new Uri("http://www.yinyuetai.com/" + page));

        }

        void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
             try
             {
                 HtmlDocument html = new HtmlDocument();
                 html.LoadHtml(e.Result);
                 var nodes = html.DocumentNode.SelectNodes("/html[1]/body[1]/div[9]/div[1]/div[1]/div[1]/ul[1]/li");

                 foreach (var node in nodes)
                 {
                     var mv = new MV();

                     mv.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(mv_PropertyChanged);

                     mv.ID = node.Element("div").Element("a").Attributes["href"].Value;
                     if (node.SelectSingleNode("div[2]/p[1]/strong[1]") != null)
                         mv.Describe = node.SelectSingleNode("div[2]/p[1]/strong[1]").InnerText;
                     //mv.Describe = node.SelectSingleNode("div[2]/p[1]").InnerText;
                     mv.Title = node.SelectSingleNode("div[2]/div[1]/a[1]").InnerText;
                     mv.Artist = node.SelectSingleNode("div[2]/div[1]/a[2]").InnerText;
                     mv.ImgSrc = node.SelectSingleNode("div[1]/a[1]/img[1]").Attributes["src"].Value;
                     list.Add(mv);
                 }
                 GlobalIndicator.Instance.IsBusy = false;
                 GlobalIndicator.Instance.Text = String.Empty;
             }
             catch (Exception)
             {

                 if (MessageBox.Show("出错了！请检查网络连接，是否查看错误信息？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                     MessageBox.Show(e.Error.Message);
                 return;
             }
          
        }

        void mv_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            act.Invoke(list);
        }
        

        List<MV> list = new List<MV>();
    }
}
